package kk.yy.utilsdesign;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.util.Log;

import java.io.File;
import java.io.IOException;

public class MediaUtils {
    private static MediaUtils mInstance;

    private MediaUtils(){

    }

    public static MediaUtils getInstance(){
        if(mInstance == null){
            mInstance=new MediaUtils();
        }
        return mInstance;
    }



    public void start(Context context,String resource){
        Log.d("MediaUtils","开始播放");
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.reset();
        AssetManager assetManager = context.getResources().getAssets();
        AssetFileDescriptor fileDescriptor = null;
        try {
            fileDescriptor = assetManager.openFd(resource);
            mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
            mediaPlayer.prepare();
            mediaPlayer.setLooping(true);
            mediaPlayer.start();
        } catch (IOException e) {
            Log.d("MediaUtils",e.toString());
            e.printStackTrace();
        }
    }




}
